package cn.echo.hrm.service.impl;

import cn.echo.hrm.contants.CourseConstants;
import cn.echo.hrm.domain.CourseType;
import cn.echo.hrm.mapper.CourseTypeMapper;
import cn.echo.hrm.service.ICourseTypeService;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 课程目录 服务实现类
 * </p>
 *
 * @author lxy
 * @since 2022-04-09
 */
//@Service
public class CourseTypeServiceImpl extends ServiceImpl<CourseTypeMapper, CourseType> implements ICourseTypeService {

    @Autowired
    private CourseTypeMapper courseTypeMapper;

    @Autowired
    private RedisTemplate redisTemplate;

    @Override
    @Cacheable(cacheNames = CourseConstants.KEY_COURSE_TYPE_TREE,key = "'ALL'")
    public List<CourseType> treeData(Long pid) {
        //代码递归
        //return getTreeDataOfRecursion(pid);
        //sql语句递归
        //return courseTypeMapper.loadTreeData(pid);
        //循环

        //缓存优化

        return getTreeDataOfLoop();

    }

    private List<CourseType> getTreeDataOfLoop() {
        //查询节点
        List<CourseType> courseTypes = courseTypeMapper.selectList(null);
        Map<Long,CourseType> courseTypesMap = new HashMap<>();
        courseTypes.forEach(courseType ->{
            courseTypesMap.put(courseType.getId(),courseType);
        } );
        //声明节点
        List<CourseType> result = new ArrayList<>();
        //遍历节点
        courseTypes.forEach(courseType -> {
            Long pidTmp = courseType.getPid();
            //判断是否为顶级节点
            if (pidTmp.longValue()==0){
                result.add(courseType);
            }else {
                //如果不是，就去查儿子
                CourseType parent = courseTypesMap.get(pidTmp);
//                CourseType parent = null;
//                for (CourseType type : courseTypes) {
//                    if (type.getId().longValue()==pidTmp.longValue()){
//                        parent = type;
//                        break;
//                    }
//                }
                parent.getChildren().add(courseType);
            }
        });
        return result;

    }

    private List<CourseType> getTreeDataOfRecursion(Long pid) {
        List<CourseType> courseTypes = courseTypeMapper.selectList(new EntityWrapper<CourseType>().eq("pid",pid));
        if (courseTypes == null || courseTypes.size()<1){
            return null;
        }
        for (CourseType type : courseTypes) {
            Long pidTmp = type.getId();
            List<CourseType> children = getTreeDataOfRecursion(pidTmp);
            type.setChildren(children);
        }

        return courseTypes;
    }

    @Override
    @CacheEvict(cacheNames = CourseConstants.KEY_COURSE_TYPE_TREE,key = "'ALL'")
    public boolean insert(CourseType entity) {
        super.insert(entity);
        return true;
    }

    @Override
    @CacheEvict(cacheNames = CourseConstants.KEY_COURSE_TYPE_TREE,key = "'ALL'")
    public boolean deleteById(Serializable id) {
        super.deleteById(id);
        return true;
    }

    @Override
    @CacheEvict(cacheNames = CourseConstants.KEY_COURSE_TYPE_TREE,key = "'ALL'")
    public boolean updateById(CourseType entity) {
        super.updateById(entity);
         return true;
    }
}
